<?php
namespace app\index\model;

date_default_timezone_set('PRC'); 
use think\Model;
use think\Db;

class Index extends Model {
    private $page = 1;
    private $limit = 12;

    public function getBrandListIndex() {
        return Db::name('brand')->where('del_flag = 0')->limit(18)->column('id,name,url');
    }

    public function getProductListIndex() {
        return Db::name('product')->where('del_flag = 0')->where('is_hot',1)->order('id desc')->limit(4)->column('id,name,abstract,img,star_price,guidance_price,discount');
    }

    public function getAllBrand() {
        return Db::name('brand')->where('del_flag = 0')->column('id,name,url');
    }

    public function getAllProduct($brandId,$q,$page) {
        $data = array();
        $limit = $this->limit;
        if (empty($page)) {
            $page = $this->page;
        } 
        $data['getPage'] = $page;
        $pageLimit = (($page - 1) * $limit ) > 0? (($page - 1) * $limit): 0;

        if ($brandId != -1 && !empty($brandId)) {
            $data['list'] = Db::name('product')
            ->where('del_flag = 0')
            ->where('brand_id', (int) $brandId)
            ->order('id desc')->limit($pageLimit,$limit)->select();
            $pageCount = count(Db::name('product')->where('del_flag = 0')->where('brand_id', (int) $brandId)->select());
            $data['brandCount'] = count(Db::name('product')->where('del_flag = 0')->where('brand_id', (int) $brandId)->select());
        } else if ($q != "" && !empty($q)){
            $res = Db::name('brand')
            ->where('del_flag = 0')
            ->where('name','like','%'.$q.'%')
            ->column('id');
            if ($res != null && !empty($res) && isset($res[0]) && $res[0] != null) {
                $brandId = $res[0];
                if ($brandId != null && !empty($brandId)) {
                    $data['list'] = Db::name('product')
                    ->where('del_flag = 0')
                    ->where('brand_id', (int) $brandId)
                    ->order('id desc')
                    ->limit($pageLimit,$limit)
                    ->column('id,name,abstract,img,star_price,guidance_price,discount');
                    $pageCount = count(Db::name('product')->where('del_flag = 0')->where('brand_id', (int) $brandId)->select());
                    $data['brandCount'] = count(Db::name('product')->where('del_flag = 0')->where('brand_id', (int) $brandId)->select());
                }
            }
            
        } else {
            $data['list'] = Db::name('product')
            ->where('del_flag = 0')
            ->order('id desc')
            ->limit($pageLimit,$limit)
            ->column('id,name,abstract,img,star_price,guidance_price,discount');
            $pageCount = count(Db::name('product')->where('del_flag = 0')->select());
        }

        $data['count'] = count(Db::name('product')->where('del_flag = 0')->select());
        
        $data['firstPage'] = 1;
        if (isset($pageCount) && $pageCount > 0) {
            $data['lastPage'] = ceil($pageCount/$limit);
        } else {
            $data['lastPage'] = ceil($data['count']/$limit);
        }
        
        if ($page < $data['firstPage']) {
            $page = $data['firstPage'];
        }
        if ($page > $data['lastPage']) {
            $page = $data['lastPage'];
        }

        $data['pageStart'] = 1;
        $data['pageEnd'] = $pageLimit + $limit;
        
        $data['thisPage'] = $page;
        $data['brandId'] = $brandId;

        return $data;
    }

    public function getProductInfo($productId) {
        return Db::name('product')
        ->where('del_flag = 0')
        ->where('id',$productId)
        ->find();
    }

    public function setIntention($data) {
        return Db::name('intention')->data($data)->insert();
    }
}